<!DOCTYPE html>
<html lang="en" xmlns:th="http://www.thymeleaf.org">
<head>
    <meta charset="utf-8">
    <title>登陆日志</title>
    <meta name="renderer" content="webkit">
    <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
    <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
    <link rel="stylesheet" th:href="@{/lib/layui/css/layui.css}" media="all">
    <link rel="stylesheet" th:href="@{/css/public.css}" media="all">

    <style>
        .layui-icon-date {
            position: absolute;
            right: 5px;
            top: 50%;
            margin-top: -15px;
            cursor: pointer;
            font-size: 30px;
            color: #C2C2C2;
        }
    </style>
</head>
<body>
<div class="layuimini-container">
    <div class="layuimini-main">

        <fieldset class="table-search-fieldset">
            <legend>搜索信息(<b>收起-展开</b>)</legend>
            <div id="search-div" style="margin: 10px 10px 10px 10px">
                <form class="layui-form" action="" lay-filter="search-form">
                    <div class="layui-form-item">

                        <div class="layui-inline">
                            <label class="layui-form-label">用户名：</label>
                            <div class="layui-input-inline">
                                <input type="text" name="account" id="account" autocomplete="off" class="layui-input" placeholder="请输入用户名">
                            </div>
                        </div>
                        <div class="layui-inline">
                            <label class="layui-form-label">登录状态：</label>
                            <div class="layui-input-inline">
                                <select name="status" id="status" lay-filter="status">
                                    <option value="">所有</option>
                                    <option th:each="item:${baseDictList}" th:text="${item.name}"
                                            th:value="${item.code}"></option>
                                </select>
                            </div>
                        </div>

                        <div class="layui-inline">
                            <label class="layui-form-label">登录时间：</label>
                            <div class="layui-input-inline" style="width: 150px">
                                <input type="text" name="startTime" id="startTime" autocomplete="off" placeholder="选择开始时间" class="layui-input" readonly>
                                <i class="layui-icon layui-icon-date"></i>
                            </div>
                            <div class="layui-form-mid">-</div>
                            <div class="layui-input-inline" style="width: 150px">
                                <input type="text" name="endTime" id="endTime" autocomplete="off" placeholder="选择结束时间" class="layui-input" readonly>
                                <i class="layui-icon layui-icon-date"></i>
                            </div>
                        </div>

                        <div class="layui-inline">
                            <button type="submit" class="layui-btn" lay-submit lay-filter="data-search-btn"><i class="layui-icon"></i> 搜 索</button>
                            <button type="submit" class="layui-btn layui-btn-primary" lay-submit lay-filter="data-reset-btn"><i class="layui-icon layui-icon-refresh"></i> 重 置 </button>
                        </div>
                    </div>
                </form>
            </div>
        </fieldset>

        <table class="layui-hide" id="currentTableId" lay-filter="currentTableFilter"></table>
    </div>
</div>

<script type="text/html" id="statusTpl">
    {{#
    if (d.status == '0') {
        return '<span class="layui-badge layui-bg-blue">成功</span>';
    } else {
        return '<span class="layui-badge layui-bg-orange">失败</span>';
    }
    }}
</script>

<script type="text/html" id="toolbarLeft">
    <div class="layui-btn-container">
        <button class="layui-btn layui-btn-sm layui-btn-danger" lay-event="batchDelete">
            <i class="layui-icon layui-icon-delete" style="font-size: 10px;"></i>批量删除
        </button>
        <button class="layui-btn layui-btn-sm layui-btn-danger" lay-event="emptyLog">
            <i class="layui-icon layui-icon-close" style="font-size: 10px;"></i>清空日志
        </button>
    </div>
</script>

<!--表格每行末尾的操作按钮-->
<script type="text/html" id="toolbarDetails">
    <a class="layui-btn layui-btn-xs" lay-event="details">详情</a>
</script>

<!-- 点击行末【详情】后的弹窗内容 -->
<div id="dom_login_details" style="display: none;">
    <div class="layuimini-main">

        <div class="layui-form-item">
            <label class="layui-form-label"><b>登陆IP：</b></label>
            <div class="layui-input-block">
                <div class="layui-form-mid" id="logIp"></div>
            </div>
        </div>
        <div class="layui-form-item">
            <label class="layui-form-label"><b>登录账户：</b></label>
            <div class="layui-input-block">
                <div class="layui-form-mid" id="logAccount"></div>
            </div>
        </div>
        <div class="layui-form-item">
            <label class="layui-form-label"><b>登录描述：</b></label>
            <div class="layui-input-block">
                <div class="layui-form-mid" id="logDescript"></div>
            </div>
        </div>

        <div class="layui-form-item">
            <label class="layui-form-label"><b>登录状态：</b></label>
            <div class="layui-input-block">
                <div class="layui-form-mid" id="logStatus"></div>
            </div>
        </div>
        <div class="layui-form-item">
            <label class="layui-form-label"><b>登录时间：</b></label>
            <div class="layui-input-block">
                <div class="layui-form-mid" id="logTime"></div>
            </div>
        </div>
        <div class="layui-form-item">
            <label class="layui-form-label"><b>登录位置：</b></label>
            <div class="layui-input-block">
                <div class="layui-form-mid" id="logLocation"></div>
            </div>
        </div>

    </div>
</div>

<script th:src="@{/lib/jquery-3.4.1/jquery-3.4.1.min.js}" charset="utf-8"></script>
<script th:src="@{/lib/layui/layui.js}" charset="utf-8"></script>
<script th:src="@{/lib/coco-message/coco-message.js}" charset="utf-8"></script>
<script th:src="@{/js/lay-config.js}" charset="utf-8"></script>
<script type="text/javascript" th:inline="javascript">
    AjaxUtil.ctx = /*[[@{/}]]*/'';
    layui.use(['form', 'table', 'laydate', 'layer'], function () {
        var form = layui.form,
            laydate = layui.laydate,
            layer= layui.layer,
            table = layui.table;

        // 执行一个laydate实例
        laydate.render({
            elem: '#startTime' //指定元素
            , eventElem: '.layui-icon-date'
            , trigger: 'click'
        });

        // 执行一个laydate实例
        laydate.render({
            elem: '#endTime' //指定元素
            , eventElem: '.layui-icon-date'
            , trigger: 'click'
        });

        var currTable = table.render({
            elem: '#currentTableId',
            url: AjaxUtil.ctx + 'loginlog/list',
            method: 'post', //如果无需自定义HTTP类型，可不加该参数
            where: {sortName:'create_time', sortOrder:'desc'}, // 初始化时带的参数
            toolbar: '#toolbarLeft',
            defaultToolbar: ['filter', 'exports', 'print'],
            cols: [
                [
                    {type: "checkbox", width: '5%', fixed: 'left'},
                    {
                        field: '', width: '4%', title: '序号', type: 'numbers', templet: function (d) {
                            return d.LAY_TABLE_INDEX + 1;
                        }
                    },
                    {field: 'ip', width: '17%', title: '登录IP', align: 'center'},
                    {field: 'account', width: '14%',title: '登录账户', sort: true},
                    {field: 'descript', width: '20%',title: '登录描述'},
                    {field: 'status', width: '8%', title: '登陆状态', templet: '#statusTpl', sort: true, align: 'center'},
                    {field: 'createTime', width: '15%', title: '登录时间', sort: true},
                    {field: 'location', width: '18%', title: '登录位置'},
                    {fixed: 'right', width: 95 ,title: '操作', align: 'center', toolbar: '#toolbarDetails'}
                ]
            ],
            autoSort: false, //禁用前端自动排序。注意：该参数为 layui 2.4.4 新增
            limits: [10, 15, 20, 25, 50, 100],
            limit: 10,
            page: true,
            skin: 'line', // 行边框风格
            parseData: function(res){ //res 即为原始返回的数据
                return {
                    "code": res.code, //解析接口状态
                    "msg": res.message, //解析提示文本
                    "count": res.data.total, //解析数据长度
                    "data": res.data.rows //解析数据列表
                };
            }
        });

        // 监听搜索操作
        form.on('submit(data-search-btn)', function (data) {
            //执行搜索重载
            currTable.reload({
                page: {
                    curr: 1 // 页码从1开始
                },
                where: form.val("search-form")
            });
            return false;
        });

        // 监听重置操作
        form.on('submit(data-reset-btn)', function (data) {
            form.val("search-form", {
                "account": '',
                "status": '',
                "startTime": '',
                "endTime": ''
            });
            // 执行搜索重载
            currTable.reload({
                page: {
                    curr: 1
                },
                where: form.val("search-form")
            });
            return false;
        });

        // 监听排序事件
        table.on('sort(currentTableFilter)', function(obj){ //注：sort 是工具条事件名，test 是 table 原始容器的属性 lay-filter="对应的值"
            console.log(obj.field); //当前排序的字段名
            console.log(obj.type); //当前排序类型：desc（降序）、asc（升序）、null（空对象，默认排序）

            //尽管我们的 table 自带排序功能，但并没有请求服务端。
            //有些时候，你可能需要根据当前排序的字段，重新向服务端发送请求，从而实现服务端排序，如：
            currTable.reload({
                initSort: obj, //记录初始排序，如果不设的话，将无法标记表头的排序状态。
                where: { //请求参数（注意：这里面的参数可任意定义，并非下面固定的格式）
                    sortName: toLine(obj.field), //排序字段
                    sortOrder: toLine(obj.type) //排序方式
                }
            });
        });


        /**
         * 【头部按钮】监听头部按钮
         */
        table.on('toolbar(currentTableFilter)', function (obj) {
            if (obj.event === 'emptyLog') {  // 监听清空日志操作
                layer.confirm('确定清空登陆日志吗？', function (index) {
                    layer.close(index);
                    AjaxUtil.post({
                        url: AjaxUtil.ctx + "loginlog/emptyLog",
                        success: function (res) {
                            if (res.code === 0) {
                                Message.success(1500, res.message, function () {
                                    currTable.reload();
                                });
                            } else {
                                Message.error(res.message, 1000);
                            }
                        },
                        error: function (error) {
                        }
                    });
                });
            } else if (obj.event === 'batchDelete') {  // 监听批量删除操作
                var checkStatus = table.checkStatus('currentTableId')
                    ,data = checkStatus.data;
                if (data == null || data.length === 0) {
                    Message.warning("请至少选择一行！", 1000);
                    return;
                }
                var logIds = [];
                for (var i = 0, len = data.length; i < len; i++) {
                    logIds.push(data[i].logId);
                }
                layer.confirm('确定批量删除登陆日志吗？', function (index) {
                    layer.close(index);
                    AjaxUtil.post({
                        url: AjaxUtil.ctx + "loginlog/batchDelete",
                        contentType: "application/json",
                        data: JSON.stringify(logIds),
                        success: function (res) {
                            if (res.code === 0) {
                                Message.success(1500, res.message, function () {
                                    currTable.reload();
                                });
                            } else {
                                Message.error(res.message, 1000);
                            }
                        },
                        error: function (error) {
                        }
                    });
                });
                // layer.alert(JSON.stringify(data));
            }
        });


        // 【行末操作】监听表格行末按钮
        table.on('tool(currentTableFilter)', function (obj) {
            let data = obj.data;
            let layEvent = obj.event;
            switch (layEvent) {
                // 【详情】按钮
                case "details":
                    layer.open({
                        type: 1,
                        shade: 0.2,
                        title: '登录日志详情',
                        skin: 'layui-layer-prompt',
                        shadeClose: true,
                        area: ['600px', '450px'],
                        content: $("#dom_login_details"),
                        success: function (layero, index) {
                            document.querySelector("#logIp").innerHTML = data.ip
                            document.querySelector("#logAccount").innerHTML = data.account
                            document.querySelector("#logDescript").innerHTML = data.descript
                            document.querySelector("#logTime").innerHTML = data.createTime
                            document.querySelector("#logLocation").innerHTML = data.location
                            if (data.status == '0') {
                                document.querySelector("#logStatus").innerHTML = '<span class="layui-badge layui-bg-blue">成功</span>'
                            } else {
                                document.querySelector("#logStatus").innerHTML = '<span class="layui-badge layui-bg-orange">失败</span>'
                            }
                        }
                    });
                    break;
            }
        });
    });
</script>

</body>
</html>